<template>
	<view>
		<u-sticky bgColor="#ffffff">
			<u-line></u-line>
			<u-tabs :list="tabs" :current="tabsCurrent" @click="tabsClick" :activeStyle="tabsActiveStyle" lineColor="#00BF9B">
			</u-tabs>
			<u-line></u-line>
		</u-sticky>
		<view class="wrapper">
			<view class="blank24"></view>
			<template v-for="(v,i) in list">
				<view class="box wrapper">
					<view class="blank20"></view>
					<view class="flex">
						<view class="text-light-gray">
							日期：
						</view>
						<view class="">
							{{v.date}}
						</view>
						<view class="flex-1"></view>
						<view class="text-light-gray">
							时间：
						</view>
						<view class="">
							{{v.dinner_type_id[1]}}
						</view>
					</view>
					<view class="blank20"></view>
					<u-line></u-line>
					<view class="blank20"></view>
					<view class="flex">
						<view class="flex-1 flex">
							<u--image :src="v.image_1920?'data:image/png;base64,'+v.image_1920:''" shape="square" radius="8" width="50" height="50"></u--image>
							<view class="ml15">
								{{v.dish_key_id[1]}}
							</view>
							<!-- <view class="mr10">
								<u--image src="https://cdn.uviewui.com/uview/album/1.jpg" shape="square" radius="8" width="50" height="50"></u--image>
								<view class="blank10"></view>
								<view class="font-24">
									午餐肉
								</view>
							</view> -->
						</view>
						<view class="">
							<text class="text-light-gray">
								数量：
							</text>
							1
						</view>
					</view>
					<view class="blank20"></view>
					<u-line></u-line>
					<view class="blank20"></view>
					<view class="flex justify-between">
						<view class="text-green font-bold">
							<!-- 未开始 -->
						</view>
						<view class="flex">
							<u-button type="error" size="small" text="删除" @click="cancel(i)"></u-button>
							<!-- <view class="mr10 ml10"></view>
							<u-button type="primary" size="mini" text="查看" @click="info(i)"></u-button> -->
						</view>
						
					</view>
					<view class="blank20"></view>
				</view>
				
				<view class="blank24"></view>
			</template>
			<view class="blank24"></view>
		</view>
		<nodata v-if="loadingStatus == 'end' && list.length == 0"></nodata>
	</view>
</template>

<script>
	import nodata from '@/components/nodata.vue';
	import {
		get_ordered_dish_data,
		common_delete
	} from '@/common/api.js';
	export default {
		components:{
			nodata
		},
		data() {
			return {
				tabs: [{
						name: '今日订单'
					},
					{
						name: '后续订单'
					},
					{
						name: '往日订单'
					},
				],
				tabsActiveStyle: {
					color: '#00BF9B',
				},
				tabsCurrent: 0,
				list:[],
				allList:[],
				sessionInfo:{},
				isLogin:false,
				phoneInfo:{},
				loadingStatus:''
			}
		},
		methods: {
			cancel(i){
				this.$store.dispatch('modal', {
					content:'确定删除该订单？',
					cancelText:'暂不删除',
					confirmText:'确定删除'
				}).then(() => {
					this.common_delete(i);
					
				}).catch(() => {
				
				}).finally(() => {
				
				})
			},
			updataList(){
				let tabsValue = this.tabs[this.tabsCurrent].name;
				if(tabsValue == '今日订单'){
					this.list = this.allList.filter(v=>v.date == this.today_year_month_day);
				}
				if(tabsValue == '后续订单'){
					this.list = this.allList.filter(v=>new Date(v.date).getTime() > new Date().getTime());
				}
				if(tabsValue == '往日订单'){
					this.list = this.allList.filter(v=>new Date(v.date).getTime() < new Date(this.today_year_month_day).getTime());
					
				}
				
			},
			common_delete(i){
				let id = this.list[i].id;
				uni.showLoading({
					title: '加载中'
				});
				let data = {};
				common_delete({
					ids:[id],
					'model_name': 'packed_order_record',
				}).then((res) => {
					this.$store.dispatch('modal', {
						content:'删除成功',
						showCancel:false
					})
					this.getData(0);
				}).catch((res) => {
					this.$store.dispatch('modal', {
						content:'删除失败',
						showCancel:false
					});
					
				}).finally(() => {
					uni.hideLoading();
				})
			},
			info(i){
				let d = JSON.stringify(this.list[i]);
				this.nav('/pages/orderDetail/orderDetail?d='+d);
			},
			tabsClick(item) {
				this.tabsCurrent = item.index;
				this.updataList();
			},
			async getData(flag){
				this.list = [];
				if(flag){
					this.loadingStatus = 'loading';
					uni.showLoading({
						title: '加载中'
					});
				}
				let data = {
					'company_id': this.company_id,
					'cost_center_id': this.cost_center_id,
					'cus_group_id': this.cus_group_id,
					'cus_loc_id': this.cus_loc_id,
					'orderer_no': this.phoneInfo.phoneNumber,
				};
				get_ordered_dish_data(data).then((res) => {
					this.allList = res.result.packed_order_record;
					this.updataList();
				
				}).catch(() => {
					uni.hideLoading();
				}).finally(() => {
					this.loadingStatus = 'end';
					uni.hideLoading();
					uni.stopPullDownRefresh();
				})
			}

		},
		async onLoad() {
			
		},
		async onShow(){
			try{
				this.sessionInfo = this.$store.getters.get_sessionInfo;
				this.isLogin = this.$store.getters.get_isLogin;
				this.phoneInfo = this.$store.getters.get_phoneInfo;
			}catch(e){}
			if(!this.isLogin){
				this.$store.dispatch('modal', {
					content:'请先授权手机号登录',
					showCancel:false
				}).then(() => {
					uni.reLaunch({
						url: '/pages/my/my'
					});
				}).catch(() => {
				
				}).finally(() => {
				
				})
				return;
			}
			if(!this.sessionInfo.company_id){
				try{
					await this.get_session_info();
					this.sessionInfo = this.$store.getters.get_sessionInfo;
				}catch(e){
					uni.$u.toast(e);
					return;
				}
				return;
			}
			this.company_id = this.sessionInfo.company_id[0];
			this.cus_loc_id = this.sessionInfo.cus_loc_id[0];
			this.cus_group_id = this.sessionInfo.cus_group_id[0];
			this.cost_center_id = this.sessionInfo.cost_center_id[0];
			this.getData(1);
		}
	}
</script>

<style>

</style>